package com.blockmeta.bbs.businesslibrary.adapter;

import android.content.Context;
import androidx.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.SpanUtils;
import com.blockmeta.bbs.baselibrary.base.adapter.BaseAdapter;
import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.baselibrary.utils.ViewUtils;
import com.blockmeta.bbs.baselibrary.utils.richtext.CacheType;
import com.blockmeta.bbs.baselibrary.utils.richtext.RichText;
import com.blockmeta.bbs.baselibrary.utils.richtext.RichTextConfig;
import com.blockmeta.bbs.businesslibrary.R;
import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.net.pojo.BBSCommentDetailPOJO;
import com.blockmeta.bbs.businesslibrary.util.RouterActivityHelper;
import com.chad.library.adapter.base.BaseViewHolder;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by author on 2018/3/21.
 */

public class AMAGuestCommentAdapter extends BaseAdapter<BBSCommentDetailPOJO.ChildBean, BaseViewHolder> {
    private Context mContext;

    public AMAGuestCommentAdapter(int layoutResId, @Nullable List<BBSCommentDetailPOJO.ChildBean> data, Context context) {
        super(layoutResId, data);
        mContext = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, final BBSCommentDetailPOJO.ChildBean item) {

        ImageView icon = helper.getView(R.id.guest_icon);
        Picasso.with(mContext).load(item.getAuthor_info().getAvatar())
                .placeholder(R.drawable.photo_default)
                .error(R.drawable.photo_default)
                .into(icon);

        icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RouterActivityHelper.openUserCenter(mContext, item.getAuthor_info().getId());
            }
        });
        helper.setText(R.id.guest_name, item.getAuthor_info().getName());
        helper.setText(R.id.guest_time, FormatUtil.StringDataToStringHMS(item.getPost_date()));
        final TextView guest_content = helper.getView(R.id.guest_content);


        RichText.from(item.getContent()).singleLoad(false).cache(CacheType.ALL).urlTagClick(mOnUrlClickListener)
                .into(guest_content);
        //复制文字！
        guest_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewUtils.copyText(mContext, guest_content.getText().toString());
            }
        });
        TextView textview = helper.getView(R.id.guest_pic);
        if (item.getAttachments().size() == 0) {
            textview.setVisibility(View.GONE);
        } else {
            textview.setText(new SpanUtils()
                                     .appendImage(mContext.getResources().getDrawable(R.drawable.comment_list_blue_pic), SpanUtils.ALIGN_CENTER)
                                     .append(" 查看图片（" + item.getAttachments().size() + "）").setForegroundColor(mContext.getResources().getColor(R.color.base_blue))
                                     .create());

            textview.setVisibility(View.VISIBLE);
        }
    }

    RichTextConfig.TagClickListener mOnUrlClickListener = new RichTextConfig.TagClickListener() {

        @Override
        public void onClickTag(String pTag, String pKey) {
            if (pTag == null) {
                return;
            }
            RouterHelper.startActivity(mContext, RouterPath.WEB_ACTIVITY,pTag);
        }
    };
}
